/**
 * losenote.com Inc.
 * Copyright (c) 2013-2013 All Rights Reserved.
 */
package com.losenote.core.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import com.losenote.core.CategoryComponent;
import com.losenote.model.AttachFile;
import com.losenote.model.CategoryItem;
import com.losenote.model.ct.CategoryItemConvertor;
import com.losenote.orm.CategoryItemDO;
import com.losenote.orm.CategoryItemDODAO;
import com.losenote.orm.CategoryItemDOExample;

/**
 * 类目分类操作组件
 * 
 * @author danjingyu
 * @version $Id: CategoryComponentImpl.java, v 0.1 2013-9-12 下午7:47:29 danjingyu Exp $
 */
public class CategoryComponentImpl implements CategoryComponent {

    /**类目dao*/
    @Autowired
    private CategoryItemDODAO categoryItemDODAO;

    /** 
     * 通过条件加载类目信息
     * 
     * @see com.losenote.core.CategoryComponent#queryCategoryByCondtion()
     */
    @SuppressWarnings("unchecked")
    public List<CategoryItem> queryCategoryChidren(int fatherId) {
        CategoryItemDOExample example = new CategoryItemDOExample();
        example.createCriteria().andFatherIdEqualTo(fatherId);
        List<CategoryItemDO> categoryItemDos = categoryItemDODAO.selectByExample(example);
        List<CategoryItem> categoryItems = new ArrayList<CategoryItem>();
        if (CollectionUtils.isEmpty(categoryItemDos)) {
            return categoryItems;
        }
        for (CategoryItemDO categoryDo : categoryItemDos) {
            categoryItems.add(consOneItem(categoryDo));
        }
        return categoryItems;
    }

    /**
     * 查询业务分类
     * @see com.losenote.core.CategoryComponent#queryCategoryId(int)
     */
    public CategoryItem queryCategoryId(int id) {
        CategoryItemDO categoryItemDO = categoryItemDODAO.selectByPrimaryKey(id);
        return consOneItem(categoryItemDO);
    }

    private CategoryItem consOneItem(CategoryItemDO categoryItemDO) {
        //【1】加载文件
        AttachFile attachFile = null;
        //【2】返回信息
        return CategoryItemConvertor.fromDo(categoryItemDO, attachFile);
    }

    public void setCategoryItemDODAO(CategoryItemDODAO categoryItemDODAO) {
        this.categoryItemDODAO = categoryItemDODAO;
    }

}
